package com.ch.web.controller;

import com.ch.web.annotation.IgnoreLogin;
import com.ch.web.requestDto.xyUser.RegisterCodeDtoForm;
import com.ch.web.redis.EmailCache;
import com.ch.web.service.EmailService;
import com.ch.web.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/email")
public class EmailSendController {

    @Autowired
    EmailService emailService;

    @Autowired
    EmailCache cache;

    @IgnoreLogin
    @PostMapping("/registerCode")
    public R sendCode(@RequestParam("email") String email) {
        //是否已经发送过了
        if (!cache.isHaveCode(email)) {
            return R.error("您的验证码已经发送过了");
        } else {
            //校验一波 因为哪怕token是有 还是会存在有些人
            return emailService.sendCode(email);
        }
    }

    @IgnoreLogin
    @PostMapping("/checkCode")
    public R checkCode(@RequestBody RegisterCodeDtoForm registerDtoForm) {
        System.out.println(registerDtoForm);
        //校验一波邮箱的过期时间
        if (cache.isNotExpire(registerDtoForm.getEmail()) && cache.matchCode(registerDtoForm)) {
            return R.ok();
        } else {
            return R.error("验证码已经过期或者不匹配");
        }
    }
}
